$(function() {
	mui.init({});
	/* 页面开始执行 */
	mui.ready(function() {
		//跳转配置页
		mui('.log-box').on('tap', 'i', function() {
			mui.openWindow({
				url: 'html/Configure/configure.html',
				id: 'configure.html'
			})
		});
		//登录  账号密码不能为空
		mui('.log-inner').on('tap', '.log-btn', function() {
			mui.showLoading("正在加载..", ".warn-list");
            var check = true;
            mui(".log-inner input.mui-input-password").each(function () {
                //若当前input为空，则alert提醒 
                if (!this.value) {
                    mui.hideLoading(function () {
                        //隐藏后的回调函数  
                        mui.alert("密码不能为空");
                        check = false;
                        return false;
                    });
                }
            });
            mui(".log-inner input.mui-input-clear").each(function () {
                //若当前input为空，则alert提醒 
                if (!this.value || this.value.trim() == "") {
                    mui.hideLoading(function () {
                        //隐藏后的回调函数  
                        mui.alert("用户名不能为空");
                        check = false;
                        return false;
                    });
                }
            });
			//校验通过，继续执行业务逻辑 
			if (check) {
				var username = $('.mui-input-clear').val();
				var pwd = $('.mui-input-password').val();
				var refor = {
					'username': username,
					'pwd': pwd,
					'type': 0
				}
				mui.showLoading("正在加载..", ".warn-list");
				var url = domain + "/dtwbgl/login.h5"
				$.ajax(url, {
					// data: JSON.stringify(refor),
					data: refor,
					dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					headers: {
						'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
					},
					async: true, //true异步加载
					success: function(data) {
						console.log(JSON.stringify(data));
						if (data.code == '1000') {						
							// 先清除
							localStorage.clear();
							var ref = data.data;
							//console.log(data);
							var user = ref.userid;
							var downloadport = ref.setting.downloadport; //下载端口
							var middleserver = ref.setting.middleserver; // 中间服务地址
							var msgport = ref.setting.msgport; //消息查询端口
							var uploadport = ref.setting.uploadport; //上传端口
							var ip = ref.yxgzsetting.ip; // 运行故障的ip
							var port = ref.yxgzsetting.port; // 运行故障的端口
							var compcode = ref.compcode; //                          
							var token = ref.token;
							// 先清除  再保存uesrId 和 token
// 							localStorage.removeItem('userid');
// 							localStorage.removeItem('token');
							localStorage.setItem('userid', user);
							localStorage.setItem('token', token);
							// localStorage.removeItem('downloadport');
							localStorage.setItem('downloadport', downloadport);
							// localStorage.removeItem('middleserver');
							localStorage.setItem('middleserver', middleserver);
							// localStorage.removeItem('msgport');
							localStorage.setItem('msgport', msgport);
							// localStorage.removeItem('uploadport');
							localStorage.setItem('uploadport', uploadport);
							// localStorage.removeItem('ip');
							localStorage.setItem('ip', ip);
							// localStorage.removeItem('port');
							localStorage.setItem('port', port);
							// localStorage.removeItem('compcode');
							localStorage.setItem('compcode', compcode);

							mui.openWindow({
								url: 'index.html',
								id: 'index.html'
							})
							console.log(data.data);

						} else if (data.code == '1001') {
							
							//隐藏加载框
							mui.hideLoading(function() { //隐藏后的回调函数  
							});
							mui.alert('用户名不存在');							
						} else if (data.code == '1002') {
							//隐藏加载框
							mui.hideLoading(function() { //隐藏后的回调函数  
							});							
							  mui.alert('账户密码错误');
						}else if (data.code == '1003') {
                            mui.hideLoading(function () {
                                //隐藏后的回调函数 
                                mui.alert('此用户无法使用本APP');
                            });
                        } else {
							//隐藏加载框
							mui.hideLoading(function() { //隐藏后的回调函数  
							});						
							 mui.alert('登录错误，请重新登录');
						}
					},
					error: function(xhr, type, errorThrown) { //异常处理； console.log(type);
						//隐藏加载框
						mui.alert(xhr);
						mui.hideLoading(function() { //隐藏后的回调函数  
						});										
					}
				});

			}
		});
	});
	/*供原生调用,用于返回*/
	function goBackByProto() {
		return propFlag;
	}
})
